#!/usr/bin/perl
#Programmer: Rory Carmichael
#Purpose: Plot rc-pipe data for a single gene using gnuplot
#NOTE: deprecated due to abandonment of p-value looper
#Usage: write_geneplot.pl <gene_record_file>
#Gene record files have the following columns in order:
#clustering probability	amino-acid	min-max-score	peak-distance

my $genefile = shift @ARGV;
chomp $genefile;
#print "$genefile\n";
my $width = `wc -l $genefile`;
chomp $width;
$width = $width - 2;
my $SX = 0.85;
my $SY = 0.25/($width/100);

print "set terminal png font \"arial,8\"\n";
print "set output '$genefile.visual.png'\n";
#print "set terminal postscript enhanced color font \"arial,8\"\n";
#print "set output '$genefile.visual.ps'\n";
print "set nokey\n";
open(IF, "$genefile");
my $line = <IF>;
$line = <IF>;
$line = <IF>;
my $i = 1.00000;
print "set xtics font \"arial,6\" (";
my @splitline = split(/\t/, $line);
print "\"" . $splitline[2] . "\" $i";
$i += 1.00000;
while (my $line = <IF>) {
	chomp $line;
#	print "$line\n";
	@splitline = split(/\t/, $line);
	print ", \"" . $splitline[2] . "\" $i";
	$i += 1.00000;
}
close(IF);
print ")\n";
print "NX=1; NY=3\n";
print "MX=0.01; MY=0.01; SX=0.85; SY=$SY\n";
print "set bmargin MY; set tmargin MY; set lmargin MX; set rmargin MX\n";
print "DX=0.1; DY=0.1\n";
print "set border 3\n";
print "set ytics nomirror\n";
print "set xtics nomirror\n";
print "set size SX*NX+DX*1.5,SY*NY+DY*1.8\n";
print "set multiplot\n";

#Bottom figure (clustering confidence)
print "set size SX,SY\n";
print "set xrange[0:$width]\n";
print "set yrange[0.00001:1.0]\n";
print "set logscale y 10\n";
print "set origin DX,DY\n";
print "plot '$genefile' using 2 with lines\n"; 

#Next figure (min-max)
print "set origin DX, DY+SY\n";
print "unset xtics\n";
print "unset logscale y\n";
print "set yrange[-100:100]\n";
print "set parametric\n";
print "set trange [-100:100]\n";
print "plot '$genefile' using 4 with lines\n";
my $i=1;
foreach my $line (`cat $genefile`) {
	chomp $line;
	my $val = [split(/\t/, $line)]->[-1];
	if ($val == 100) {
		print "plot $i,t\n";
	}
	$i++;
}
print "set nomultiplot\n";
